﻿using Dapper.ExpressionExtension.ExpressionMappers;
using Dapper.ExpressionExtension.SqlBuilders;
using System;
using System.Diagnostics;

namespace Sample
{
    class Program
    {
        public static bool First = true;

        static void Main()
        {
            A();
            Console.ReadLine();
        }


        public static void Select()
        {
            var sqlBuild = new MySqlSqlBuilder();
            var expression = new SqlExpression<Person>(sqlBuild);
            var sql = expression.Select().ToSql();
            if (First)
            {
                Console.WriteLine(sql);
                First = false;
            }
        }

        public static void SelectDto()
        {
            var sqlBuild = new MySqlSqlBuilder();
            var expression = new SqlExpression<Person>(sqlBuild);
            var sql = expression.Select(x => new Person
            {
                Id = x.Id,
                Name = x.Name,
                WeddingDay = x.WeddingDay,
                Age = x.Age,
            }).Where(x => x.Id == 1 && x.Name.StartsWith("12")).OrderBy(x => x.Age).ToSql();
            if (First)
            {
                Console.WriteLine(sql);
                First = false;
            }
        }

        public static void Update()
        {
            var sqlBuild = new MySqlSqlBuilder();
            var expression = new SqlExpression<Person>(sqlBuild);
            var person = new Person
            {
                Id = 1,
                Age = 2,
                Name = "frank",
                WeddingDay = null
            };
            var sql = expression
                .Set(x => x.Age, person.Age)
                .Set(x => x.Name, "aa")
                .Where(x => x.Id == 1 && x.Name.StartsWith("12"))
                .ToSql();
            //var sql = expression.Update(x => new Person
            //{
            //    Age = person.Age,
            //    Name = "2"
            //})
            //    .Where(x => x.Id == 1 && x.Name.StartsWith("12"))
            //    .ToSql();

            if (First)
            {
                Console.WriteLine(sql);
                First = false;
            }
        }

        public static void A()
        {
            Update();
            var a = new Stopwatch();
            a.Start();
            for (int i = 0; i < 10000; i++)
            {
                Update();
            }
            a.Stop();
            var b = a.ElapsedMilliseconds;
            Console.WriteLine($"take {b}");
        }

        public static void From()
        {
            var sqlBuild = new MySqlSqlBuilder();
            var expression = new SqlExpression<Person>(sqlBuild);
            var sql = ExpressionMapper.SelectAdvance(expression, sqlExpression =>
                sqlExpression.Select(p => new Person { Id = p.Id })
                    .Where(p => p.Name!.StartsWith("Chai") && p.Age == 1)
                    .OrWhere(p => p.Name != null)
                    .AndWhere(p => p.Age != 0)
                    .OrderBy(p => p.Age)
                    .OrderByDescending(p => p.Name)
                    .Page(1, 5), out var parameters);
            if (First)
            {
                Console.WriteLine(sql);
                First = false;
            }
        }
    }
}
